Fix gnttab_release_mappings -- it doesn't need to drop
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Wed, 21 Dec 2005 17:45:43 +0000 (18:45 +0100)
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Wed, 21 Dec 2005 17:45:43 +0000 (18:45 +0100)
page refcnts for host mappings as they are already
destroyed by put_page_from_l1e(). Also call
gnttab_release_mappings later (after destroying
page-table references) and from common code rather than
arch/x86.

Also a few other misc gnttab cleanups.

Signed-off-by: Keir Fraser <keir@xensource.com>
xen/arch/x86/domain.c
xen/arch/x86/setup.c
xen/common/domain.c
xen/common/grant_table.c
xen/include/xen/grant_table.h

index 983ce0fe8fa371b37f3050097e39ce95c6da158f..4c586226dd58092640cba3036dbe6f5b089e9aa8 100644 (file)
@@ -957,8 +957,6 @@ void domain_relinquish_resources(struct domain *d)
 
     ptwr_destroy(d);
 
-    gnttab_release_mappings(d);
-
     /* Drop the in-use references to page-table bases. */
     for_each_vcpu ( d, v )
     {
index 70a22d5a76c240d191da4b7811a4d4abdbb50013..1259e095e8465d590df94dcb4d37cb3f63ccd7e9 100644 (file)
@@ -488,8 +488,6 @@ void __init __start_xen(multiboot_info_t *mbi)
 
     start_of_day();
 
-    grant_table_init();
-
     shadow_mode_init();
 
     /* initialize access control security module */
index 9d2806d9ee33cb9deff021a3df372248a447839c..bc54c313de41519c4d3f854596c9f4d8deb4dc3c 100644 (file)
@@ -118,6 +118,7 @@ void domain_kill(struct domain *d)
         for_each_vcpu(d, v)
             sched_rem_domain(v);
         domain_relinquish_resources(d);
+        gnttab_release_mappings(d);
         put_domain(d);
 
         send_guest_virq(dom0->vcpu[0], VIRQ_DOM_EXC);
index 156b43e80207bfa013fcfa468af178a158a0b06a..4622007c6a85c2cc28afc0310599c212357d1a62 100644 (file)
@@ -614,6 +614,91 @@ gnttab_dump_table(
     return 0;
 }
 
+/*
+ * Check that the given grant reference (rd,ref) allows 'ld' to transfer
+ * ownership of a page frame. If so, lock down the grant entry.
+ */
+static int 
+gnttab_prepare_for_transfer(
+    struct domain *rd, struct domain *ld, grant_ref_t ref)
+{
+    grant_table_t *rgt;
+    grant_entry_t *sha;
+    domid_t        sdom;
+    u16            sflags;
+    u32            scombo, prev_scombo;
+    int            retries = 0;
+    unsigned long  target_pfn;
+
+    if ( unlikely((rgt = rd->grant_table) == NULL) ||
+         unlikely(ref >= NR_GRANT_ENTRIES) )
+    {
+        DPRINTK("Dom %d has no g.t., or ref is bad (%d).\n",
+                rd->domain_id, ref);
+        return 0;
+    }
+
+    spin_lock(&rgt->lock);
+
+    sha = &rgt->shared[ref];
+    
+    sflags = sha->flags;
+    sdom   = sha->domid;
+
+    for ( ; ; )
+    {
+        target_pfn = sha->frame;
+
+        if ( unlikely(target_pfn >= max_page ) )
+        {
+            DPRINTK("Bad pfn (%lx)\n", target_pfn);
+            goto fail;
+        }
+
+        if ( unlikely(sflags != GTF_accept_transfer) ||
+             unlikely(sdom != ld->domain_id) )
+        {
+            DPRINTK("Bad flags (%x) or dom (%d). (NB. expected dom %d)\n",
+                    sflags, sdom, ld->domain_id);
+            goto fail;
+        }
+
+        /* Merge two 16-bit values into a 32-bit combined update. */
+        /* NB. Endianness! */
+        prev_scombo = scombo = ((u32)sdom << 16) | (u32)sflags;
+
+        /* NB. prev_scombo is updated in place to seen value. */
+        if ( unlikely(cmpxchg_user((u32 *)&sha->flags, prev_scombo, 
+                                   prev_scombo | GTF_transfer_committed)) )
+        {
+            DPRINTK("Fault while modifying shared flags and domid.\n");
+            goto fail;
+        }
+
+        /* Did the combined update work (did we see what we expected?). */
+        if ( likely(prev_scombo == scombo) )
+            break;
+
+        if ( retries++ == 4 )
+        {
+            DPRINTK("Shared grant entry is unstable.\n");
+            goto fail;
+        }
+
+        /* Didn't see what we expected. Split out the seen flags & dom. */
+        /* NB. Endianness! */
+        sflags = (u16)prev_scombo;
+        sdom   = (u16)(prev_scombo >> 16);
+    }
+
+    spin_unlock(&rgt->lock);
+    return 1;
+
+ fail:
+    spin_unlock(&rgt->lock);
+    return 0;
+}
+
 static long
 gnttab_transfer(
     gnttab_transfer_t *uop, unsigned int count)
@@ -762,87 +847,6 @@ do_grant_table_op(
     return rc;
 }
 
-int 
-gnttab_prepare_for_transfer(
-    struct domain *rd, struct domain *ld, grant_ref_t ref)
-{
-    grant_table_t *rgt;
-    grant_entry_t *sha;
-    domid_t        sdom;
-    u16            sflags;
-    u32            scombo, prev_scombo;
-    int            retries = 0;
-    unsigned long  target_pfn;
-
-    if ( unlikely((rgt = rd->grant_table) == NULL) ||
-         unlikely(ref >= NR_GRANT_ENTRIES) )
-    {
-        DPRINTK("Dom %d has no g.t., or ref is bad (%d).\n",
-                rd->domain_id, ref);
-        return 0;
-    }
-
-    spin_lock(&rgt->lock);
-
-    sha = &rgt->shared[ref];
-    
-    sflags = sha->flags;
-    sdom   = sha->domid;
-
-    for ( ; ; )
-    {
-        target_pfn = sha->frame;
-
-        if ( unlikely(target_pfn >= max_page ) )
-        {
-            DPRINTK("Bad pfn (%lx)\n", target_pfn);
-            goto fail;
-        }
-
-        if ( unlikely(sflags != GTF_accept_transfer) ||
-             unlikely(sdom != ld->domain_id) )
-        {
-            DPRINTK("Bad flags (%x) or dom (%d). (NB. expected dom %d)\n",
-                    sflags, sdom, ld->domain_id);
-            goto fail;
-        }
-
-        /* Merge two 16-bit values into a 32-bit combined update. */
-        /* NB. Endianness! */
-        prev_scombo = scombo = ((u32)sdom << 16) | (u32)sflags;
-
-        /* NB. prev_scombo is updated in place to seen value. */
-        if ( unlikely(cmpxchg_user((u32 *)&sha->flags, prev_scombo, 
-                                   prev_scombo | GTF_transfer_committed)) )
-        {
-            DPRINTK("Fault while modifying shared flags and domid.\n");
-            goto fail;
-        }
-
-        /* Did the combined update work (did we see what we expected?). */
-        if ( likely(prev_scombo == scombo) )
-            break;
-
-        if ( retries++ == 4 )
-        {
-            DPRINTK("Shared grant entry is unstable.\n");
-            goto fail;
-        }
-
-        /* Didn't see what we expected. Split out the seen flags & dom. */
-        /* NB. Endianness! */
-        sflags = (u16)prev_scombo;
-        sdom   = (u16)(prev_scombo >> 16);
-    }
-
-    spin_unlock(&rgt->lock);
-    return 1;
-
- fail:
-    spin_unlock(&rgt->lock);
-    return 0;
-}
-
 int 
 grant_table_create(
     struct domain *d)
@@ -943,7 +947,8 @@ gnttab_release_mappings(
             {
                 BUG_ON(!(act->pin & GNTPIN_hstr_mask));
                 act->pin -= GNTPIN_hstr_inc;
-                put_page(pfn_to_page(act->frame));
+                /* Done implicitly when page tables are destroyed. */
+                /* put_page(pfn_to_page(act->frame)); */
             }
         }
         else
@@ -959,7 +964,8 @@ gnttab_release_mappings(
             {
                 BUG_ON(!(act->pin & GNTPIN_hstw_mask));
                 act->pin -= GNTPIN_hstw_inc;
-                put_page_and_type(pfn_to_page(act->frame));
+                /* Done implicitly when page tables are destroyed. */
+                /* put_page_and_type(pfn_to_page(act->frame)); */
             }
 
             if ( (act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0 )
@@ -982,24 +988,17 @@ void
 grant_table_destroy(
     struct domain *d)
 {
-    grant_table_t *t;
+    grant_table_t *t = d->grant_table;
 
-    if ( (t = d->grant_table) != NULL )
-    {
-        /* Free memory relating to this grant table. */
-        d->grant_table = NULL;
-        free_xenheap_pages(t->shared, ORDER_GRANT_FRAMES);
-        free_xenheap_page(t->maptrack);
-        xfree(t->active);
-        xfree(t);
-    }
-}
+    if ( t == NULL )
+        return;
+    
+    free_xenheap_pages(t->shared, ORDER_GRANT_FRAMES);
+    free_xenheap_page(t->maptrack);
+    xfree(t->active);
+    xfree(t);
 
-void
-grant_table_init(
-    void)
-{
-    /* Nothing. */
+    d->grant_table = NULL;
 }
 
 /*
index df5e3d4c54a1c343d9884c2532d9e1e82a8b8e53..266110154696686869fe9e58e5bbe8010b8aefb9 100644 (file)
@@ -84,24 +84,12 @@ typedef struct {
     spinlock_t            lock;
 } grant_table_t;
 
-/* Start-of-day system initialisation. */
-void grant_table_init(
-    void);
-
 /* Create/destroy per-domain grant table context. */
 int grant_table_create(
     struct domain *d);
 void grant_table_destroy(
     struct domain *d);
 
-/*
- * Check that the given grant reference (rd,ref) allows 'ld' to transfer
- * ownership of a page frame. If so, lock down the grant entry.
- */
-int 
-gnttab_prepare_for_transfer(
-    struct domain *rd, struct domain *ld, grant_ref_t ref);
-
 /* Domain death release of granted mappings of other domains' memory. */
 void
 gnttab_release_mappings(